<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身高" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入身高"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体重" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入体重"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体脂率" prop="bodyFatPercentage">
        <el-input
          v-model="queryParams.bodyFatPercentage"
          placeholder="请输入体脂率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="腰围" prop="waistCircumference">
        <el-input
          v-model="queryParams.waistCircumference"
          placeholder="请输入腰围"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血压" prop="bloodPressure">
        <el-input
          v-model="queryParams.bloodPressure"
          placeholder="请输入血压"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="心率" prop="heartRate">
        <el-input
          v-model="queryParams.heartRate"
          placeholder="请输入心率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血糖水平" prop="bloodSugarLevel">
        <el-input
          v-model="queryParams.bloodSugarLevel"
          placeholder="请输入血糖水平"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="胆固醇水平" prop="cholesterolLevel">
        <el-input
          v-model="queryParams.cholesterolLevel"
          placeholder="请输入胆固醇水平"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血红蛋白水平" prop="hemoglobinLevel">
        <el-input
          v-model="queryParams.hemoglobinLevel"
          placeholder="请输入血红蛋白水平"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="骨密度" prop="boneDensity">
        <el-input
          v-model="queryParams.boneDensity"
          placeholder="请输入骨密度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="睡眠质量描述" prop="sleepQuality">
        <el-input
          v-model="queryParams.sleepQuality"
          placeholder="请输入睡眠质量描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="心理健康评估结果" prop="mentalHealthAssessment">
        <el-input
          v-model="queryParams.mentalHealthAssessment"
          placeholder="请输入心理健康评估结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="吸烟习惯" prop="smokingHabits">
        <el-input
          v-model="queryParams.smokingHabits"
          placeholder="请输入吸烟习惯"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="饮酒习惯" prop="alcoholConsumption">
        <el-input
          v-model="queryParams.alcoholConsumption"
          placeholder="请输入饮酒习惯"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="健康评分" prop="healthScore">
        <el-input
          v-model="queryParams.healthScore"
          placeholder="请输入健康评分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录日期" prop="recordDate">
        <el-date-picker clearable
          v-model="queryParams.recordDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择记录日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['assessment:UserHealthData:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['assessment:UserHealthData:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['assessment:UserHealthData:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['assessment:UserHealthData:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="UserHealthDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户健康数据ID" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="身高" align="center" prop="height" />
      <el-table-column label="体重" align="center" prop="weight" />
      <el-table-column label="体脂率" align="center" prop="bodyFatPercentage" />
      <el-table-column label="腰围" align="center" prop="waistCircumference" />
      <el-table-column label="血压" align="center" prop="bloodPressure" />
      <el-table-column label="心率" align="center" prop="heartRate" />
      <el-table-column label="血糖水平" align="center" prop="bloodSugarLevel" />
      <el-table-column label="胆固醇水平" align="center" prop="cholesterolLevel" />
      <el-table-column label="血红蛋白水平" align="center" prop="hemoglobinLevel" />
      <el-table-column label="骨密度" align="center" prop="boneDensity" />
      <el-table-column label="饮食记录" align="center" prop="dietaryRecord" />
      <el-table-column label="运动记录" align="center" prop="exerciseRecord" />
      <el-table-column label="睡眠质量描述" align="center" prop="sleepQuality" />
      <el-table-column label="心理健康评估结果" align="center" prop="mentalHealthAssessment" />
      <el-table-column label="家族病史" align="center" prop="familyHistory" />
      <el-table-column label="过敏史" align="center" prop="allergyHistory" />
      <el-table-column label="吸烟习惯" align="center" prop="smokingHabits" />
      <el-table-column label="饮酒习惯" align="center" prop="alcoholConsumption" />
      <el-table-column label="药物使用情况" align="center" prop="medicationUsage" />
      <el-table-column label="健康评分" align="center" prop="healthScore" />
      <el-table-column label="记录日期" align="center" prop="recordDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备用列1" align="center" prop="spareColumn1" />
      <el-table-column label="备用列2" align="center" prop="spareColumn2" />
      <el-table-column label="备用列3" align="center" prop="spareColumn3" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['assessment:UserHealthData:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['assessment:UserHealthData:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改健康数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="身高" prop="height">
          <el-input v-model="form.height" placeholder="请输入身高" />
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重" />
        </el-form-item>
        <el-form-item label="体脂率" prop="bodyFatPercentage">
          <el-input v-model="form.bodyFatPercentage" placeholder="请输入体脂率" />
        </el-form-item>
        <el-form-item label="腰围" prop="waistCircumference">
          <el-input v-model="form.waistCircumference" placeholder="请输入腰围" />
        </el-form-item>
        <el-form-item label="血压" prop="bloodPressure">
          <el-input v-model="form.bloodPressure" placeholder="请输入血压" />
        </el-form-item>
        <el-form-item label="心率" prop="heartRate">
          <el-input v-model="form.heartRate" placeholder="请输入心率" />
        </el-form-item>
        <el-form-item label="血糖水平" prop="bloodSugarLevel">
          <el-input v-model="form.bloodSugarLevel" placeholder="请输入血糖水平" />
        </el-form-item>
        <el-form-item label="胆固醇水平" prop="cholesterolLevel">
          <el-input v-model="form.cholesterolLevel" placeholder="请输入胆固醇水平" />
        </el-form-item>
        <el-form-item label="血红蛋白水平" prop="hemoglobinLevel">
          <el-input v-model="form.hemoglobinLevel" placeholder="请输入血红蛋白水平" />
        </el-form-item>
        <el-form-item label="骨密度" prop="boneDensity">
          <el-input v-model="form.boneDensity" placeholder="请输入骨密度" />
        </el-form-item>
        <el-form-item label="饮食记录" prop="dietaryRecord">
          <el-input v-model="form.dietaryRecord" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="运动记录" prop="exerciseRecord">
          <el-input v-model="form.exerciseRecord" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="睡眠质量描述" prop="sleepQuality">
          <el-input v-model="form.sleepQuality" placeholder="请输入睡眠质量描述" />
        </el-form-item>
        <el-form-item label="心理健康评估结果" prop="mentalHealthAssessment">
          <el-input v-model="form.mentalHealthAssessment" placeholder="请输入心理健康评估结果" />
        </el-form-item>
        <el-form-item label="家族病史" prop="familyHistory">
          <el-input v-model="form.familyHistory" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="过敏史" prop="allergyHistory">
          <el-input v-model="form.allergyHistory" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="吸烟习惯" prop="smokingHabits">
          <el-input v-model="form.smokingHabits" placeholder="请输入吸烟习惯" />
        </el-form-item>
        <el-form-item label="饮酒习惯" prop="alcoholConsumption">
          <el-input v-model="form.alcoholConsumption" placeholder="请输入饮酒习惯" />
        </el-form-item>
        <el-form-item label="药物使用情况" prop="medicationUsage">
          <el-input v-model="form.medicationUsage" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="健康评分" prop="healthScore">
          <el-input v-model="form.healthScore" placeholder="请输入健康评分" />
        </el-form-item>
        <el-form-item label="记录日期" prop="recordDate">
          <el-date-picker clearable
            v-model="form.recordDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择记录日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备用列1" prop="spareColumn1">
          <el-input v-model="form.spareColumn1" placeholder="请输入备用列1" />
        </el-form-item>
        <el-form-item label="备用列2" prop="spareColumn2">
          <el-input v-model="form.spareColumn2" placeholder="请输入备用列2" />
        </el-form-item>
        <el-form-item label="备用列3" prop="spareColumn3">
          <el-input v-model="form.spareColumn3" placeholder="请输入备用列3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserHealthData, getUserHealthData, delUserHealthData, addUserHealthData, updateUserHealthData } from "@/api/assessment/UserHealthData";

export default {
  name: "UserHealthData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 健康数据表格数据
      UserHealthDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        height: null,
        weight: null,
        bodyFatPercentage: null,
        waistCircumference: null,
        bloodPressure: null,
        heartRate: null,
        bloodSugarLevel: null,
        cholesterolLevel: null,
        hemoglobinLevel: null,
        boneDensity: null,
        dietaryRecord: null,
        exerciseRecord: null,
        sleepQuality: null,
        mentalHealthAssessment: null,
        familyHistory: null,
        allergyHistory: null,
        smokingHabits: null,
        alcoholConsumption: null,
        medicationUsage: null,
        healthScore: null,
        recordDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询健康数据列表 */
    getList() {
      this.loading = true;
      listUserHealthData(this.queryParams).then(response => {
        this.UserHealthDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        height: null,
        weight: null,
        bodyFatPercentage: null,
        waistCircumference: null,
        bloodPressure: null,
        heartRate: null,
        bloodSugarLevel: null,
        cholesterolLevel: null,
        hemoglobinLevel: null,
        boneDensity: null,
        dietaryRecord: null,
        exerciseRecord: null,
        sleepQuality: null,
        mentalHealthAssessment: null,
        familyHistory: null,
        allergyHistory: null,
        smokingHabits: null,
        alcoholConsumption: null,
        medicationUsage: null,
        healthScore: null,
        recordDate: null,
        spareColumn1: null,
        spareColumn2: null,
        spareColumn3: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加健康数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserHealthData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改健康数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUserHealthData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserHealthData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除健康数据编号为"' + ids + '"的数据项？').then(function() {
        return delUserHealthData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('assessment/UserHealthData/export', {
        ...this.queryParams
      }, `UserHealthData_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
